const { createApp } = Vue

// 配置 axios 默认值和拦截器
axios.defaults.baseURL = '/api';

// 请求拦截器
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (!token) {
        window.location.href = '/login.html';
        return Promise.reject('未登录');
    }
    config.headers['Authorization'] = token;
    return config;
}, error => {
    return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(
    response => response,
    error => {
        if (error.response?.status === 401) {
            localStorage.removeItem('token');
            window.location.href = '/login.html';
        }
        return Promise.reject(error);
    }
);

createApp({
    data() {
        return {
            loading: false,
            statusFilter: 'all',
            showDetailModal: false,
            selectedRecord: null,
            currentPage: 1,
            pageSize: 10,
            totalRecords: [],
            displayRecords: [],
            statusOptions: [
                { value: 'all', label: '全部状态' },
                { value: 'cancelled', label: '已取消' },
                { value: 'unbooked', label: '未预约' },
                { value: 'pending', label: '待进行' },
                { value: 'completed', label: '已完成' }
            ]
        }
    },
    computed: {
        filteredRecords() {
            let result = this.totalRecords;

            if (this.statusFilter !== 'all') {
                result = result.filter(record => record.status === this.statusFilter);
            }

            return result;
        },

        totalPages() {
            return Math.ceil(this.filteredRecords.length / this.pageSize);
        },

        paginatedRecords() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            return this.filteredRecords.slice(start, end);
        }
    },
    methods: {
        // 获取咨询记录
        async fetchRecords() {
            try {
                this.loading = true;
                const response = await axios.get('/therapist/records');

                if (response.data.code === "200") {
                    this.totalRecords = response.data.data;
                    this.updateDisplayRecords();
                } else {
                    throw new Error(response.data.message);
                }
            } catch (error) {
                console.error('获取咨询记录失败:', error);
                if (!error.message.includes('未登录')) {
                    alert(error.response?.data?.message || '获取咨询记录失败，请稍后重试');
                }
            } finally {
                this.loading = false;
            }
        },
        updateDisplayRecords() {
            this.displayRecords = this.paginatedRecords;
        },
        changePage(page) {
            if (page >= 1 && page <= this.totalPages) {
                this.currentPage = page;
                this.updateDisplayRecords();
            }
        },
        // 搜索记录
        searchRecords() {
            this.currentPage = 1;
            this.updateDisplayRecords();
        },

        // 查看记录详情
        viewRecordDetail(record) {
            this.selectedRecord = record;
            this.showDetailModal = true;
        },

        // 关闭详情模态框
        closeDetailModal() {
            this.showDetailModal = false;
            this.selectedRecord = null;
        },

        // 获取状态文本
        getStatusText(status) {
            const statusMap = {
                'cancelled': '已取消',
                'unbooked': '未预约',
                'pending': '待进行',
                'completed': '已完成'
            };
            return statusMap[status] || status;
        },

        // 获取状态标签的样式类
        getStatusClass(status) {
            return {
                'status-tag': true,
                'cancelled': status === 'cancelled',
                'unbooked': status === 'unbooked',
                'pending': status === 'pending',
                'completed': status === 'completed'
            };
        }
    },
    created() {
        this.fetchRecords();
    }
}).mount('#app') 